/*
 * Project:		ListenMyStory
 * Purpose:		Preference
 * 				[+] Android Publish Book (Image + Text)
 * 				 - Autobiography(UCC, Let me tell you a little something about me)
 * 				 - Novel (General, Game Novel, ...)
 * 				 - Gallery (Pictures + Descriptions)
 * Author:		Ho-Jung Kim (godmode2k@hotmail.com)
 * Date:		Since September 15, 2010
 * Latest modified:	June 09, 2011
 * Filename:	AppPreferenceActivity.java
 * 
 * Source:
 * Note:
 * ----------------------------------------
 * TODO:
 * 		- Menu Dialog {
 * 			1. Bookmark
 * 			2. Audio Volume
 * 			3. Contents List (goto) [OK]
 * 			4. Font Size [OK]
 * 			5. Export
 * 			6. Set Resource path [OK]
 * 			7. Load Contents(.zip) from SD card
 * 			x. ...
 * 		}
 *
 * URGENT!!!
 * TODO:
 * 
 * License:
	This file is part of ListenMyStory.
	ListenMyStory is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	ListenMyStory is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with ListenMyStory.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.JustListen.ListenMyStory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView;
import android.widget.AbsListView;
import android.widget.TextView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

// DIP to Pixel
import android.util.TypedValue;

public class AppPreferenceActivity extends Activity {
	private String TAG = "AppPreferenceActivity";
	private static final boolean m_isDebugOn = true;
	private ListenMyStoryApp m_MainApp = null;
	
	// -----------------------------
	// Preference ListView
	// -----------------------------
	private ListView m_ListView = null;
	private SimpleAdapter m_ListViewAdapter = null;
	private static final int __PREFERENCE_MENU__1__ = 0;
	private static final int __PREFERENCE_MENU__2__ = 1;
	private static final int __PREFERENCE_MENU__3__ = 2;
	private static final int __PREFERENCE_MENU__4__ = 3;
	private static final int __PREFERENCE_MENU__5__ = 4;
	
	// -----------------------------
	// Contents List ListView
	// -----------------------------
	private ListView m_ListViewResContents = null;
	private SimpleAdapter m_ListViewResContentsAdapter = null;
	
	// -----------------------------
	// Haptic
	// -----------------------------
	Vibrator m_Vibrator = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Log.d( TAG, "onCreate()" );
        
        setContentView( R.layout.main_app_preference_activity );

        // Get Main Class of the Application
        m_MainApp = (ListenMyStoryApp)getApplicationContext();

        // Haptic
        m_Vibrator = (Vibrator)this.getSystemService( VIBRATOR_SERVICE );
        
        // ListView
        init_listview();
    }
    
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    	
        Log.d( TAG, "onDestroy()" );
        
        if( m_MainApp != null )
        	m_MainApp.SetSaveConf();
    }
    
    /*
     * Activity Status
		run1: app
			onStart -> onResume
		
		run2-1: app -> no play -> home
			onSaveInstanceState -> onStop
		run2-2: app
			onStop -> onRestart -> onStart -> onResume
		
		run3-1: play -> home
			onSaveInstanceState -> onStop
		run3-2: app
			onRestart -> onStart -> onResume
    */

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        
        Log.d( TAG, "onConfigurationChanged()" );
    }
    
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        
        Log.d( TAG, "onRestoreInstanceState()" );
    }

    public void onRestart(){
        super.onRestart();

        Log.d( TAG, "onRestart()" );
    }

    public void onStart(){
        super.onStart();
        
        Log.d( TAG, "onStart()" );
    }
   
    public void onResume(){
        super.onResume();
        
        Log.d( TAG, "onResume()" );
        
    	//Intent i = new Intent( "com.android.music.musicservicecommand" );
    	//i.putExtra( "command", "pause" );
    	////i.putExtra( "_ListenMyStory_", "_ListenMyStory_" );
    	//sendBroadcast( i );
    }
    
    public void onSaveInstanceState(Bundle savedInstanceState){
        super.onSaveInstanceState(savedInstanceState);
        
        Log.d( TAG, "onSaveInstanceState()" );
    }
    
    public void onPuase() {
        super.onPause();

        Log.d( TAG, "onPuase()" );
    }

    public void onStop() {
        super.onStop();

        Log.d( TAG, "onStop()" );
    }
    
    public void __DEBUG__(String str) {
    	if( m_isDebugOn )
    		Log.d( TAG, str );
    }
    
    public void __DEBUG_ERROR__(String str) {
   		Log.d( TAG, str );
    }

    // ---------------------------------------------------------
    
// ---------------------------------------------
// Dialog Message Box
// ---------------------------------------------
    // Show Failure Message Box
	public void DlgFail(String strTitle, String strMessage) {
		final Builder winAlert;
 		Dialog winDialog;
     	LayoutInflater li = LayoutInflater.from( this );
     	View viewDlgFail = li.inflate( R.layout.dlg_messagebox, null );
     	
     	__DEBUG__( "DlgFail()" );    	

     	// Back Key
     	DialogInterface.OnKeyListener keyListener = new DialogInterface.OnKeyListener() {
 			@Override
 			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
 				// TODO Auto-generated method stub
 				
 				if( keyCode == KeyEvent.KEYCODE_BACK )
 					return true;

 				return false;
 			}
 		};

     	DialogInterface.OnClickListener okListener = new DialogInterface.OnClickListener() {
 			public void onClick(DialogInterface dialog, int which) {
 				// TODO Auto-generated method stub
 				
 				// OK
 				if( which == -1 ) {
 					__DEBUG__( "DlgFail(): Button: SELECTED #1 OK" );
 					//Toast.makeText( AppPreferenceActivity.this, "...", Toast.LENGTH_SHORT ).show();
 				}
 				// Cancel #1
 				else if( which == -3 ) {
 					__DEBUG__( "DlgFail(): Button: SELECTED #2 Cancel" );
 				}
 				// Cancel #2
 				else if( which == -2 ) {
 					__DEBUG__( "DlgFail(): Button: SELECTED #3 (Cancel)" );				
 				}				
 				
 				dialog.cancel();
 				
 				__DEBUG__( "DlgFail(): Finish: =====Exit App=====" );
 				System.exit( 1 );
 				return;
 			}
 		};
 		
 		winAlert = new AlertDialog.Builder( this )
 		.setIcon( R.drawable.icon )
 		.setOnKeyListener( keyListener )
 		//.setTitle( getResources().getString(R.string.dlgFailTitle) )
 		//.setMessage( AppPreferenceActivity.this.m_strResMsg )
 		.setTitle( strTitle )
 		.setMessage( strMessage )
 		.setPositiveButton( getResources().getString(R.string.dlgBtn_OK), okListener )
 		.setView( viewDlgFail );
 		winDialog = winAlert.create();
 		winDialog.show();
    }

	// Show Message Box
    public void DlgMessageBox(String strTitle, String strMessage) {
 		final Builder winAlert;
 		Dialog winDialog;
     	LayoutInflater li = LayoutInflater.from( this );
     	View viewDlgMessageBox = li.inflate( R.layout.dlg_messagebox, null );
     	
     	__DEBUG__( "DlgMessageBox()" );

     	// Back Key
     	DialogInterface.OnKeyListener keyListener = new DialogInterface.OnKeyListener() {
 			@Override
 			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
 				// TODO Auto-generated method stub
 				
 				if( keyCode == KeyEvent.KEYCODE_BACK )
 					return true;

 				return false;
 			}
 		};
     	
     	DialogInterface.OnClickListener okListener = new DialogInterface.OnClickListener() {
 			public void onClick(DialogInterface dialog, int which) {
 				// TODO Auto-generated method stub
 				
 				// OK
 				if( which == -1 ) {
 					__DEBUG__( "DlgMessageBox(): Button: SELECTED #1 OK" );
 					//Toast.makeText( AppPreferenceActivity.this, "...", Toast.LENGTH_SHORT ).show();
 				}
 				// Cancel #1
 				else if( which == -3 ) {
 					__DEBUG__( "DlgMessageBox(): Button: SELECTED #2 Cancel" );
 				}
 				// Cancel #2
 				else if( which == -2 ) {
 					__DEBUG__( "DlgMessageBox(): Button: SELECTED #3 (Cancel)" );				
 				}				
 				
 				dialog.cancel();
 				return;
 			}
 		};
 		
 		winAlert = new AlertDialog.Builder( this )
	 		.setIcon( R.drawable.icon )
	 		.setOnKeyListener( keyListener )
	 		//.setTitle( getResources().getString(R.string.dlgFailTitle) )
	 		//.setMessage( AppPreferenceActivity.this.m_strResMsg )
	 		.setTitle( strTitle )
	 		.setMessage( strMessage )
	 		.setPositiveButton( getResources().getString(R.string.dlgBtn_OK), okListener )
	 		.setNegativeButton( getResources().getString(R.string.dlgBtn_CANCEL), okListener )
	 		.setView( viewDlgMessageBox );
 		winDialog = winAlert.create();
 		winDialog.show();
    }

    // Set a Resource (Contents) Path Dialog
    public void DlgResPath(String strTitle, String strMessage) {
  		final Builder winAlert;
  		Dialog winDialog;
      	LayoutInflater li = LayoutInflater.from( this );
      	final View viewDlg = li.inflate( R.layout.dlg_res_path, null );
      	
      	__DEBUG__( "DlgResPath()" );

      	// Resource Path Message
      	{
      		TextView tv = (TextView)viewDlg.findViewById( R.id.TextView_ResPathMessage );
      		if( tv != null ) {
      			if( strMessage.equals("") )
      				tv.setText( "Resource Path:" );
      			else
      				tv.setText( strMessage );
      		}
      		
      		EditText et = (EditText)viewDlg.findViewById( R.id.EditTextDlgResPath );
       		if( et != null ) {
				if( m_MainApp != null ) {
					String strResPath = m_MainApp.GetConfManager().GetResPath();
					if( !strResPath.equals(et.getText()) ) {
						et.setText( strResPath );
					}
				}
				else {
					__DEBUG_ERROR__( "DlgResPath(): m_MainApp == NULL" );
					return;
				}
       		}
     	}

      	// Back Key
      	DialogInterface.OnKeyListener keyListener = new DialogInterface.OnKeyListener() {
  			@Override
  			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
  				// TODO Auto-generated method stub
  				
  				if( keyCode == KeyEvent.KEYCODE_BACK )
  					return true;

  				return false;
  			}
  		};
      	
      	DialogInterface.OnClickListener okListener = new DialogInterface.OnClickListener() {
  			public void onClick(DialogInterface dialog, int which) {
  				// TODO Auto-generated method stub
  				
  				// OK
  				if( which == -1 ) {
  					__DEBUG__( "DlgResPath(): Button: SELECTED #1 OK" );
  					//Toast.makeText( AppPreferenceActivity.this, "...", Toast.LENGTH_SHORT ).show();
  					
  					EditText et = (EditText)viewDlg.findViewById( R.id.EditTextDlgResPath );
  					if( et != null ) {
  						__DEBUG__( "DlgResPath(): Resource Path = " + et.getText().toString() );
  						
  						if( m_MainApp != null ) {
  							m_MainApp.GetConfManager().SetResLocation( et.getText().toString() );
  							m_MainApp.SetCreatesResDirectory();
  							m_MainApp.SetSaveConf();
  						}
  						else {
  							__DEBUG_ERROR__( "DlgResPath(): m_MainApp == NULL" );
  							return;
  						}
  					}
  				}
  				// Cancel #1
  				else if( which == -3 ) {
  					__DEBUG__( "DlgResPath(): Button: SELECTED #2 Cancel" );
  				}
  				// Cancel #2
  				else if( which == -2 ) {
  					__DEBUG__( "DlgResPath(): Button: SELECTED #3 (Cancel)" );				
  				}				
  				
  				dialog.cancel();
  				return;
  			}
  		};
  		
  		winAlert = new AlertDialog.Builder( this )
	  		.setIcon( R.drawable.icon )
	  		.setOnKeyListener( keyListener )
	  		//.setTitle( getResources().getString(R.string.dlgFailTitle) )
	  		//.setMessage( AppPreferenceActivity.this.m_strResMsg )
	  		.setTitle( strTitle )
	  		//.setMessage( strMessage )
	  		.setPositiveButton( getResources().getString(R.string.dlgBtn_OK), okListener )
	  		.setNegativeButton( getResources().getString(R.string.dlgBtn_CANCEL), okListener )
	  		.setView( viewDlg );
  		winDialog = winAlert.create();
  		winDialog.show();
    }

    // Set a Font Size Dialog
    public void DlgFontSize(String strTitle, String strMessage) {
   		final Builder winAlert;
   		Dialog winDialog;
       	LayoutInflater li = LayoutInflater.from( this );
       	final View viewDlg = li.inflate( R.layout.dlg_font_size, null );
       	
       	__DEBUG__( "DlgFontSize()" );

       	// Font Size Message
       	{
       		TextView tv = (TextView)viewDlg.findViewById( R.id.TextView_FontSizeMessage );
       		if( tv != null ) {
       			if( strMessage.equals("") )
       				tv.setText( "Font Size: (0 to 25)" );
       			else
       				tv.setText( strMessage );
       		}
       		
       		EditText et = (EditText)viewDlg.findViewById( R.id.EditTextDlgFontSize );
       		if( et != null ) {
				if( m_MainApp != null ) {
					String strFontSize = m_MainApp.GetConfManager().GetFontSize();
					if( !strFontSize.equals(et.getText()) ) {
						et.setText( strFontSize );
					}
				}
				else {
					__DEBUG_ERROR__( "DlgFontSize(): m_MainApp == NULL" );
					return;
				}
       		}
      	}

       	// Back Key
       	DialogInterface.OnKeyListener keyListener = new DialogInterface.OnKeyListener() {
   			@Override
   			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
   				// TODO Auto-generated method stub
   				
   				if( keyCode == KeyEvent.KEYCODE_BACK )
   					return true;

   				return false;
   			}
   		};
       	
       	DialogInterface.OnClickListener okListener = new DialogInterface.OnClickListener() {
   			public void onClick(DialogInterface dialog, int which) {
   				// TODO Auto-generated method stub
   				
   				// OK
   				if( which == -1 ) {
   					__DEBUG__( "DlgFontSize(): Button: SELECTED #1 OK" );
   					//Toast.makeText( AppPreferenceActivity.this, "...", Toast.LENGTH_SHORT ).show();
   					
   					EditText et = (EditText)viewDlg.findViewById( R.id.EditTextDlgFontSize );
   					if( et != null ) {
   						__DEBUG__( "Font Size = " + et.getText().toString() );
   						
   						int fontSize = 0;
   						
   						fontSize = Integer.parseInt( et.getText().toString() );
   						if( fontSize > 25 ) {
   							//Toast.makeText( AppPreferenceActivity.this, "Font Size = 0 ~ 25", Toast.LENGTH_SHORT ).show();
   							__DEBUG_ERROR__( "DlgFontSize(): Font Size (max 25) = " + fontSize );
   							return;
   						}
   						
   						if( m_MainApp != null ) {
   							m_MainApp.GetConfManager().SetFontSize( fontSize );
   							m_MainApp.SetSaveConf();
   						}
   						else {
   							__DEBUG_ERROR__( "DlgFontSize(): m_MainApp == NULL" );
   							return;
   						}
   					}
   				}
   				// Cancel #1
   				else if( which == -3 ) {
   					__DEBUG__( "DlgFontSize(): Button: SELECTED #2 Cancel" );
   				}
   				// Cancel #2
   				else if( which == -2 ) {
   					__DEBUG__( "DlgFontSize(): Button: SELECTED #3 (Cancel)" );				
   				}				
   				
   				dialog.cancel();
   				return;
   			}
   		};
   		
   		winAlert = new AlertDialog.Builder( this )
 	  		.setIcon( R.drawable.icon )
 	  		.setOnKeyListener( keyListener )
 	  		//.setTitle( getResources().getString(R.string.dlgFailTitle) )
 	  		//.setMessage( AppPreferenceActivity.this.m_strResMsg )
 	  		.setTitle( strTitle )
 	  		//.setMessage( strMessage )
 	  		.setPositiveButton( getResources().getString(R.string.dlgBtn_OK), okListener )
 	  		.setNegativeButton( getResources().getString(R.string.dlgBtn_CANCEL), okListener )
 	  		.setView( viewDlg );
   		winDialog = winAlert.create();
   		winDialog.show();
    }

    // Show Contents List Dialog
    public void DlgContentsList(String strTitle, String strMessage) {
   		final Builder winAlert;
   		Dialog winDialog;
       	LayoutInflater li = LayoutInflater.from( this );
       	final View viewDlg = li.inflate( R.layout.dlg_contents_listview, null );
       	
       	__DEBUG__( "DlgContentsList()" );

       	// Contents List Message
       	{
       		TextView tv = (TextView)viewDlg.findViewById( R.id.TextView_Message );
       		if( tv != null ) {
       			if( strMessage.equals("") )
       				tv.setText( "List of Contents" );
       			else
       				tv.setText( strMessage );
       		}
      	}

       	// Back Key
       	DialogInterface.OnKeyListener keyListener = new DialogInterface.OnKeyListener() {
   			@Override
   			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
   				// TODO Auto-generated method stub
   				
   				if( keyCode == KeyEvent.KEYCODE_BACK )
   					return true;

   				return false;
   			}
   		};
       	
       	DialogInterface.OnClickListener okListener = new DialogInterface.OnClickListener() {
   			public void onClick(DialogInterface dialog, int which) {
   				// TODO Auto-generated method stub
   				
   				// OK
   				if( which == -1 ) {
   					__DEBUG__( "DlgContentsList(): Button: SELECTED #1 OK" );
   					//Toast.makeText( AppPreferenceActivity.this, "...", Toast.LENGTH_SHORT ).show();
   				}
   				// Cancel #1
   				else if( which == -3 ) {
   					__DEBUG__( "DlgContentsList(): Button: SELECTED #2 Cancel" );
   				}
   				// Cancel #2
   				else if( which == -2 ) {
   					__DEBUG__( "DlgContentsList(): Button: SELECTED #3 (Cancel)" );				
   				}				
   				
   				dialog.cancel();
   				return;
   			}
   		};
   		
   	    {
   	    	__DEBUG__( "DlgContentsList(): Init Resource List of Contents" );
   	    	
   	    	/*
   	    		// Layout for List of Contents
	   	    	+R.layout.dlg_contents_listview
	   	    	 - R.id.ListView_ContentsList
	   	    	+R.layout.dlg_contents_listview_item
	   	    	 - R.id.TextView_Contents_Item
   	    	*/
   	    	
   	    	m_ListViewResContents = (ListView)viewDlg.findViewById( R.id.ListView_ContentsList );

   	    	ArrayList<HashMap<String, String>> itemList = new ArrayList<HashMap<String, String>>();
   	    	
   	    	/*
   	    	// Row 1
   	    	HashMap<String, String> map = new HashMap<String, String>();
   	    	map.put( "menu", "item 1" );
   	    	itemList.add( map );

   	    	// Row 2
   	    	map = new HashMap<String, String>();
   	    	map.put( "menu", "item 2" );
   	    	itemList.add( map );

   	    	// Row 3
   	    	map = new HashMap<String, String>();
   	    	map.put( "menu", "item 3" );
   	    	itemList.add( map );

   	    	// Row 4
   	    	map = new HashMap<String, String>();
   	    	map.put( "menu", "item 4" );
   	    	itemList.add( map );

   	    	// Row 5
   	    	map = new HashMap<String, String>();
   	    	map.put( "menu", "item 5" );
   	    	itemList.add( map );
			*/

   	    	if( m_MainApp == null ) {
   	    		__DEBUG_ERROR__( "DlgContentsList(): m_MainApp == NULL" );
   	    		return;
   	    	}

   	    	for( int i = 0; i < m_MainApp.GetXmlResManage().size(); i++ ) {
   	    		//__DEBUG__( "DlgContentsList(): " + m_MainApp.GetXmlResManage().get(i).GetNum() + " " + m_MainApp.GetXmlResManage().get(i).GetTitle() );
   	    		
   	   	    	HashMap<String, String> map = new HashMap<String, String>();
   	   	    	map.put( "num", m_MainApp.GetXmlResManage().get(i).GetNum() );
   	   	    	map.put( "item", m_MainApp.GetXmlResManage().get(i).GetTitle() );
   	   	    	itemList.add( map );
   	    	}

   	    	m_ListViewResContentsAdapter = new SimpleAdapter( this, itemList, R.layout.dlg_contents_listview_item,
   	    			new String[] {"num", "item"},
   	    			new int[] {R.id.TextView_Contents_Num, R.id.TextView_Contents_Item});
   	    	m_ListViewResContents.setAdapter( m_ListViewResContentsAdapter );
   	    	m_ListViewResContents.setTextFilterEnabled( true );
   	   		
   	   		/*
   	   		m_ListViewResContents.setOnScrollListener( new OnScrollListener() {
   				@Override
   				public void onScrollStateChanged(AbsListView view, int scrollState) {
   					// TODO Auto-generated method stub
   					switch( scrollState ) {
   						case SCROLL_STATE_FLING:
   						case SCROLL_STATE_TOUCH_SCROLL:
   						case SCROLL_STATE_IDLE:
   							break;
   					}
   				}
   				
   				@Override
   				public void onScroll(AbsListView view, int firstVisibleItem,
   						int visibleItemCount, int totalItemCount) {
   					// TODO Auto-generated method stub
   					
   					if( (firstVisibleItem+visibleItemCount) == (totalItemCount - 1) ) {
   						// ...
   					}
   					
   					if( m_ListViewResContents.getLastVisiblePosition() == (m_ListViewResContents.getCount() - 1) ) {
   						// ...
   					}
   				}
   			});
   			*/

   	    	m_ListViewResContents.setOnItemClickListener( new OnItemClickListener() {
   				@Override
   				public void onItemClick(AdapterView<?> a, View arg1, int position, long id) {
   					// TODO Auto-generated method stub
   					
   					__DEBUG__( "DlgContentsList(): onItemClick(): Selected Item Position = " + position + ", data = " + m_ListViewResContents.getItemAtPosition(position) );
   					m_Vibrator.vibrate( 20 );
   					
   					// position start at '0'
   				}
   			});
   	    }
   		
   		winAlert = new AlertDialog.Builder( this )
	   		.setIcon( R.drawable.icon )
	   		.setOnKeyListener( keyListener )
	   		//.setTitle( getResources().getString(R.string.dlgFailTitle) )
	   		//.setMessage( AppPreferenceActivity.this.m_strResMsg )
	   		.setTitle( strTitle )
	   		//.setMessage( strMessage )
	   		.setPositiveButton( getResources().getString(R.string.dlgBtn_OK), okListener )
	   		.setNegativeButton( getResources().getString(R.string.dlgBtn_CANCEL), okListener )
	   		.setView( viewDlg );
   		winDialog = winAlert.create();
   		winDialog.show();
    }

    public void init_listview() {
    	Log.d( TAG, "init_listview()" );
    	
// Has a column
/*
		m_ListView = (ListView)findViewById( R.id.ListView_Preference );
		ArrayList<HashMap<String, String>> itemList = new ArrayList<HashMap<String, String>>();

  		HashMap<String, String> map = new HashMap<String, String>();
    	map.put( "num", "01" );
    	map.put( "title", "__TITLE__" );
    	map.put( "note", "__NOTE__" );
    	itemList.add( map );

    	// Row 2
    	map = new HashMap<String, String>();
    	map.put( "num", "02" );
    	map.put( "title", "__TITLE__" );
    	map.put( "note", "__NOTE__" );
    	itemList.add( map );
    	...
    	
    	m_ListViewAdapter = new SimpleAdapter( this, itemList, R.layout.main_app_preference_listview,
    		new String[] {"num", "title", "note"},
    		new int[] {R.id.TextViewListViewMedia_num, R.id.TextViewListView_Title, R.id.TextViewListView_Note});
    	m_ListView.setAdapter( m_ListViewAdapter );
    	m_ListView.setTextFilterEnabled( true );

*/
    	
    	/*
    	//main_app_preference_listview
    	String[] list = new String[] { "Menu1", "Menu2", "Menu3", "Menu4" };
    	ListView lv = new ListView( this );
    	ArrayAdapter lv_adapter = new ArrayAdapter<String>( this, R.layout.ListView_Preference_Item, lv );
    	lv.setAdapter( lv_adapter );
    	*/
    	
    	m_ListView = (ListView)findViewById( R.id.ListView_Preference );

// FooterView example
/*
		// MUST be put on the top as a member variable
    	LinearLayout m_LinearLayoutFooterView = null;
    	ImageView m_ImageViewFooter = new ImageView( this );

    	// DIP to PIXEL #1
		// final float scale = getContext().getResources().getDisplayMetrics().density;
		// mGestureThreshold = (int) (GESTURE_THRESHOLD_DIP * scale + 0.5f);
		//
		// DIP to PIXEL (123.4dp to pixel) #2
		// int value = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float)123.4, getResources().getDisplayMetrics());
		int dipToPixel = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float)7.7, getResources().getDisplayMetrics());
    	
    	m_ImageViewFooter.setLayoutParams( new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, dipToPixel) );
    	if( m_LinearLayoutFooterView != null ) {
    		m_LinearLayoutFooterView.removeAllViews();
    		m_LinearLayoutFooterView = null;
    	}
    	m_LinearLayoutFooterView = new LinearLayout( this );
    	m_LinearLayoutFooterView.addView( m_ImageViewFooter );
    	m_ListView.addFooterView( m_LinearLayoutFooterView );
*/

    	ArrayList<HashMap<String, String>> itemList = new ArrayList<HashMap<String, String>>();
    	
    	// Row 1
    	HashMap<String, String> map = new HashMap<String, String>();
    	map.put( "menu", "List of Contents" );
    	itemList.add( map );

    	// Row 2
    	map = new HashMap<String, String>();
    	map.put( "menu", "Font Size" );
    	itemList.add( map );

    	// Row 3
    	map = new HashMap<String, String>();
    	map.put( "menu", "Resource Path" );
    	itemList.add( map );

    	// Row 4
    	map = new HashMap<String, String>();
    	map.put( "menu", "Make a contents" );
    	itemList.add( map );

    	// Row 5
    	map = new HashMap<String, String>();
    	map.put( "menu", "About this App" );
    	itemList.add( map );

    	m_ListViewAdapter = new SimpleAdapter( this, itemList, R.layout.main_app_preference_menu_listview,
    			new String[] {"menu"},
    			new int[] {R.id.TextView_Preference_Item});
   		m_ListView.setAdapter( m_ListViewAdapter );
   		m_ListView.setTextFilterEnabled( true );
   		
   		/*
   		m_ListView.setOnScrollListener( new OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub
				switch( scrollState ) {
					case SCROLL_STATE_FLING:
					case SCROLL_STATE_TOUCH_SCROLL:
					case SCROLL_STATE_IDLE:
						break;
				}
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub
				
				if( (firstVisibleItem+visibleItemCount) == (totalItemCount - 1) ) {
					// ...
				}
				
				if( m_ListView.getLastVisiblePosition() == (m_ListView.getCount() - 1) ) {
					// ...
				}
			}
		});
		*/

   		m_ListView.setOnItemClickListener( new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> a, View arg1, int position, long id) {
				// TODO Auto-generated method stub
				
				Log.d( TAG, "onItemClick(): Selected Item Position = " + position + ", data = " + m_ListView.getItemAtPosition(position) );
				m_Vibrator.vibrate( 20 );
				
				// position start at '0'
				switch( position ) {
					case __PREFERENCE_MENU__1__:
						// List of Contents
						{
							String strTitle = "" + getResources().getText( R.string.app_name );
							String str = "List of Contents";
							DlgContentsList( strTitle, str );
						}
						break;
					case __PREFERENCE_MENU__2__:
						// Font Size
						{
							String strTitle = "" + getResources().getText( R.string.app_name );
							String str = "Font Size: (0 to 25)";
							DlgFontSize( strTitle, str );
						}
						break;
					case __PREFERENCE_MENU__3__:
						// Resource Path
						{
							String strTitle = "" + getResources().getText( R.string.app_name );
							String str = "Resource Path:"
								+ "\n" + "   (It will be create a directory into"
								+ "\n" + "    your external storage such as"
								+ "\n" + "    \"/sdcard/\" or \"/mnt/sdcard/\")";
							DlgResPath( strTitle, str );
						}
						break;
					case __PREFERENCE_MENU__4__:
						// Make a contents
						{
							//Toast.makeText( AppPreferenceActivity.this, "Not Available for now. It will be supports later.", Toast.LENGTH_LONG ).show();
							Toast.makeText( AppPreferenceActivity.this, "Not Available for now.", Toast.LENGTH_SHORT ).show();
						}
						break;
					case __PREFERENCE_MENU__5__:
						// About this App
						{
							String strTitle = "" + getResources().getText( R.string.app_name );
							String strVersion = "" + getResources().getText( R.string.app_version );
							String str =
								"+Author: \n"
								+ "   Ho-Jung Kim \n"
								+ "   (godmode2k@hotmail.com)"
								+ "\n\n" + "+Date: \n"
								+ "   Since June 09, 2011"
								+ "\n\n" + "+Version: \n"
								+ "   " + strVersion;
							DlgMessageBox( strTitle,  str );
						}
						break;
				}
			}
		});
    }
/*
    public class CSimpleAdapter extends SimpleAdapter {
        private Activity m_Activity;
	    //private String[] data;
	    //private static LayoutInflater inflater = null;
	    //public ImageLoader imageLoader;
        
        //public CSimpleAdapter(Context context,
		//		List<? extends Map<String, ?>> data, int resource,
		//		String[] from, int[] to) {
		//	super(context, data, resource, from, to);
		//	// TODO Auto-generated constructor stub
		//}
		
		public CSimpleAdapter( Activity a, List<? extends Map<String, ?>> data, int resource,
				String[] from, int[] to) {
			super( a, data, resource, from, to );
			// TODO Auto-generated constructor stub
			
			m_Activity = a;
		}
		
		//public LazyAdapter(Activity a, String[] d) {
	    //    activity = a;
	    //    data=d;
	    //    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    //    imageLoader=new ImageLoader(activity.getApplicationContext());
	    //}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			//View view = super.getView( position, convertView, parent );
			//return view;

			// Example)
	        //View vi = convertView;
	        //public static class ViewHolder {
	        //    public TextView text;
	        //    public ImageView image;
	        //}
	        //ViewHolder holder;
	        //if( convertView == null ) {
	        //    vi = inflater.inflate( R.layout.item, null );
	        //    holder = new ViewHolder();
	        //    holder.text = (TextView)vi.findViewById( R.id.text );
	        //    holder.image = (ImageView)vi.findViewById( R.id.image );
	        //    vi.setTag( holder );
	        //}
	        //else
	        //    holder = (ViewHolder)vi.getTag();
	        //
	        //holder.text.setText( "item " + position );
	        //holder.image.setTag( data[position] );
	        //imageLoader.DisplayImage( data[position], activity, holder.image );
	        //return vi;
			
			//View view = convertView;
			View view = super.getView( position, convertView, parent );
	        if( convertView == null )
	            view = inflater.inflate( R.layout.main_app_preference_listview, null );

            TextView tv = (TextView)view.findViewById( R.id.TextView_Preference_Item );
	        tv.setText( "item " + position );
	        
	        
	        // MARQUEE
			//TextView1.setEllipsize( TruncateAt.MARQUEE );
			//TextView1.setFocusable( true );
	        
	        
	        return view;
		}
    }
*/
}
